package com.fizzkai.p2p.core.util;

import com.fizzkai.p2p.core.VerifyCodeVo;
import com.fizzkai.p2p.core.domain.LoginInfo;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public abstract class UserContext {
    public static final String USER_IN_SESSION = "USER_IN_SESSION";
    public static final String VERIFYCODE_IN_SESSION = "VERIFYCODE_IN_SESSION";

    // 获取 请求
    private static HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }
    // 获取 Session
    private static HttpSession getSession() {
        return getRequest().getSession();
    }
    // 设置 属性 和 返回 属性 员工
    public static LoginInfo getCurrentLoginInfo(){
        return (LoginInfo) getSession().getAttribute(USER_IN_SESSION);
    }
    public static void setCurrentLoginInfo(LoginInfo e){
        getSession().setAttribute(USER_IN_SESSION, e);
        getSession().setAttribute("loginInfo", e);
    }
    // 设置 验证
    public static VerifyCodeVo getVerifyCodeVo() {
        return (VerifyCodeVo) getSession().getAttribute(VERIFYCODE_IN_SESSION);
    }

    // 获取 IP
    public static String getIp(){
        return getRequest().getRemoteAddr();
    }

    public static void setVerifyCodeVo(VerifyCodeVo e) {
        getSession().setAttribute(VERIFYCODE_IN_SESSION, e);
    }
}
